<template>
<div>
  <router-link to="/" tag="div" class="header-abs" v-show="showAbs">
    <span class="iconfont header-abs-back">&#xe624;</span>
  </router-link>
  <div 
  class="header-fixed" 
  v-show="!showAbs"
  :style="fixedOpacity"
  >
    景点详情
    <router-link to="/">
      <div class="iconfont header-back">&#xe624;</div>
    </router-link>
  </div>
</div>
  
</template>
<script>
export default {
  name: "DetailHeader",
  data() {
    return {
      showAbs: true,
      fixedOpacity: {
        opacity: 0
      }
    };
  },
  methods: {
    handleScroll() {
      let top = document.documentElement.scrollTop;
      if (top > 60) {
        this.showAbs = false;
        let opacity = top / 140;
        opacity = opacity > 1 ? 1 : opacity;
        this.fixedOpacity = {
          opacity
        };
      } else {
        this.showAbs = true;
      }
    }
  },
  activated() {
    window.addEventListener("scroll", this.handleScroll);
  },
  deactivated(){
    // 用了keepalive的时候, 在组件隐藏的时候的钩子函数, 因为给window绑定了全局事件, 所以当组件隐藏的是应该给winddow绑定的事件解绑, 从而不影响其他组件
    window.removeEventListener("scroll", this.handleScroll);
  }
};
</script>
<style lang="stylus" scoped>
.header-abs {
  position: absolute;
  left: 0.2rem;
  top: 0.2rem;
  width: 0.8rem;
  height: 0.8rem;
  background: rgba(0, 0, 0, 0.8);
  color: #fff;
  text-align: center;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;

  .header-abs-back {
    width: 100%;
  }
}

@import '~styles/varibles.styl';

.header-fixed {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  overflow: hidden;
  height: $headerHeight;
  line-height: $headerHeight;
  text-align: center;
  color: #ffffff;
  background-color: $bgColor;
  font-size: 0.32rem;

  .header-back {
    position: absolute;
    overflow: hidden;
    width: 0.64rem;
    font-size: 0.4rem;
    left: 0;
    top: 0;
    color: #fff;
  }
}
</style>
